<template>
	<view>
		<u-notify ref="uNotify" message="Hi uView" style="z-index: 999;"></u-notify>
		<u-upload style="width: 100%;"  :header="headers" @afterRead="afterRead" max-count="1" :multiple="false" :before-upload="handleFileChange">
			<u-button class="blue_btn" style="width: 100%;background: #2F6CBC;border: 1px solid #2F6CBC;"
				type="primary"><text style="width: 120px;">无犯罪记录证明</text></u-button>
		</u-upload>
<!-- 		<el-upload class="upload-demo" :on-success="handleAvatarSuccess"  :action="action"
			:headers="headers" :before-upload="handleFileChange" :show-file-list="false" style="width: 100%;">
			<el-button class="blue_btn" style="width: 100%;background: #2F6CBC;border: 1px solid #2F6CBC;"
				type="primary">无犯罪证明</el-button>
		</el-upload> -->
	</view>
</template>
<script>
	import {
		URL_IP_HOST,
		URL_PREFIX
	} from '@/util/constant.js'
	import {
		parserResume
	} from "@/api/my.js"
	export default {
		data() {
			var userInfoData_ = uni.getStorageSync("userInfoData_")
			return {
				headers: {
					"Authorization": `Bearer ${userInfoData_ ? userInfoData_.access_token : ""}`,
					"clientid": userInfoData_ ? userInfoData_.client_id : "eeb9377d4b6debbc0a6e8756bbbbb4c7"
				},
				action: URL_IP_HOST+"/application/oss/upload",
			};
		},
		methods: {
			async afterRead(event) {
				console.log(event);
				let rawFile=event.file.url.split(".")[1]
				// console.log("上传前钩子函数:",event,rawFile);
				if (rawFile !== 'jpeg'   && rawFile !== 'jpg' && rawFile !== 'png'&& rawFile !== 'PNG' && rawFile !== 'JPG') {
					this.$refs.uNotify.show({
						top: 0.1,
						type: 'error',
						color: '#ffffff',
						bgColor: '#f56c6c',
						message:  `格式有误! 仅支持jpg、png、jpeg格式!`,
						duration: 1000 * 2,
						fontSize: 16,
						safeAreaInsetTop: false
					})
					return 
				}
				const result = await this.uploadFilePromise(event.file.url)
				let obj=JSON.parse(result.data).data
				this.$emit("uploadImageClick",obj.ossId)
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.action, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header:this.headers,
						formData: {
							user: 'test'
						},
						success: (res) => {
							
							setTimeout(() => {
								resolve(res)
							}, 1000)
						}
					});
				})
			},
			handleAvatarSuccess(res, file) {
				console.log("文件上传成功：", res, file);
				this.$emit("uploadImageClick",res.data.ossId)
			},
			handleFileChange(rawFile) {
				console.log(rawFile);
				// if (rawFile.type !== 'application/msword' &&rawFile.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' &&rawFile.type !== 'application/pdf' && rawFile.type !== 'image/jpg' && rawFile.type !== 'image/png') {
				// 	this.$message({
				// 		message: '格式有误! 仅支持jpg、png、doc、docx、pdf格式!',
				// 		type: 'error',
				// 		offset: 0.1
				// 	});
				// 	return false
				// }
				if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/jpg' && rawFile.type !== 'image/png') {
					// this.$message({
					// 	message: '格式有误! 仅支持jpg、png格式!',
					// 	type: 'error',
					// 	offset: 0.1
					// });
					this.$refs.uNotify.show({
						top: 0.1,
						type: 'error',
						color: '#ffffff',
						bgColor: '#f56c6c',
						message:  `格式有误! 仅支持jpg、png格式!`,
						duration: 1000 * 2,
						fontSize: 16,
						safeAreaInsetTop: false
					})
					return false
				}
			}
		}
	}
</script>
<style lang="scss" scoped>

	.blue_btn {
		background: #2F6CBC;
		border: 1px solid #2F6CBC;
	}
</style>
